package cc.yugu.yhdw.webserver.web.controller.security.basic;


import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.DictItem;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.service.basic.DictItemService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * 字典控制器
 * 
 * @Date:Created in 2018/7/23 14:22
 * @Author:
 * @Modified By: zjn 2018/7/23 14:22
 */ 
@RequestMapping(value = "/security/basic/dict_item")
@Controller
public class DictItemController extends SecurityController {

    @Autowired
    DictItemService dictItemService;

    @RequestMapping("index.htm")
    public String index() {
        return "/security/basic/dict_item/index";
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/23  14:22
    */ 
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(DictItem search) {
        return PageResult.successResult(dictItemService.findPage(search));
    }

    @RequestMapping("add.htm")
    @ViewModel(ViewModel.INNER_PAGE)
    public void add(){

    }
    /**
    * 后台页面新建，插入数据库
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  14:23
    */ 
    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(DictItem entity) {
        return dictItemService.insert(entity);
    }
    /**
    * 点击修改，弹出页面
    * 
    * @Param: [id, model]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/23  14:23
    */ 
    @RequestMapping("edit.htm")
    @ViewModel(ViewModel.INNER_PAGE)
    public String edit(int id, Model model) {
        DictItem entity = dictItemService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        model.addAttribute("entity", entity);
        return "/security/basic/dict_item/edit";
    }
    /**
    * 更新数据库
    * 
    * @Param: [dictItem]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  14:24
    */ 
    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(DictItem dictItem) {
        return dictItemService.update(dictItem);
    }
    /**
    * 点击查看，弹出页面
    * 
    * @Param: [id, model]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/23  14:24
    */ 
    @RequestMapping("view.htm")
    @ViewModel(ViewModel.INNER_PAGE)
    public String view(int id, Model model) {
        DictItem entity = dictItemService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        model.addAttribute("entity", entity);
        return "/security/basic/dict_item/view";
    }
    /**
    * 删除
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  14:25
    */
    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        return dictItemService.delete(id);
    }

    @RequestMapping("find_category.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public void find_category(HttpServletResponse response) throws IOException {
        response.setContentType(ConstEnum.ContentType.JSON.getValue());
        dictItemService.dictCategoryJson(response.getOutputStream());
    }
}
